package com.strelitzia.admin.controller;

import com.strelitzia.admin.Entity.NotificationEntity;
import com.strelitzia.admin.Entity.UserEntity;
import com.strelitzia.admin.service.NotificationService;
import com.strelitzia.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @create 2024/1/24 10:10
 */

@Controller
public class NotificationController {
    @Autowired
    NotificationService notificationService;

    @Autowired
    UserService userService;

    @GetMapping("/notification")
    public String notification(Model model){
        List<UserEntity> list = userService.list();
        model.addAttribute("list",list);
        return "notification";
    }

    @PostMapping("/notification")
    public String notification(String content,Integer receiver){
        NotificationEntity notificationEntity = new NotificationEntity();
        notificationEntity.setMessage("系统消息: "+content);
        notificationEntity.setNoRead(0);
        notificationEntity.setNotifier(0);
        notificationEntity.setTime(new Timestamp(new Date().getTime()));
        notificationEntity.setType(3);
        if (receiver == 0) {
            List<UserEntity> list = userService.list();
            for (UserEntity userEntity : list) {
                notificationEntity.setReceiver(userEntity.getId());
                notificationService.save(notificationEntity);
            }
        }else {
            notificationEntity.setReceiver(receiver);
            notificationService.save(notificationEntity);
        }
        return "redirect:/notification";
    }
}
